package com.exsun.ex1077.ui.popup

import android.content.Context
import com.exsun.ex1077.R
import com.lxj.xpopup.core.CenterPopupView
import com.zhangping.fastjetpack.extend.showToast
import com.zhangping.fastjetpack.extend.view.textString
import com.zhangping.fastjetpack.model.KeyValue
import kotlinx.android.synthetic.main.popup_bounds_add.view.*

/**
 * 描述：围栏保存弹窗
 * 作者：ZhangPing
 * 创建时间：2023/3/22 13:57
 */
class BoundsSavePopup(context: Context,
                      private val content: String? = null,
                      private val onSelectCompanyListener: () -> Unit,
                      private val onInputConfirmListener: (popupView: CenterPopupView, content: String, company: KeyValue) -> Unit): CenterPopupView(context) {

    override fun getImplLayoutId() = R.layout.popup_bounds_add

    private var boundsName: String? = null
    private var companyData: KeyValue? = null

    override fun onCreate() {
        super.onCreate()
        content?.let {
            nameEdit.setText(it)
            nameEdit.setSelection(it.length)
        }
        boundsName?.let {
            nameEdit.setText(it)
            nameEdit.setSelection(it.length)
        }
        companyData?.let { companySelect.text = it.value }
        companySelect.setOnClickListener { onSelectCompanyListener.invoke() }
        confirm.setOnClickListener {
            if (nameEdit.textString().isEmpty()){
                "请输入围栏名称".showToast()
                return@setOnClickListener
            }
            if (companyData == null){
                "请选择所属公司".showToast()
                return@setOnClickListener
            }
            dismiss()
            onInputConfirmListener.invoke(this, nameEdit.textString(), companyData!!)
        }
        cancel.setOnClickListener { dismiss() }
    }

    fun getBoundsName() = nameEdit?.text.toString()

    fun getCompanyData() = companyData

    fun setCompanyData(data: KeyValue){
        this.companyData = data
        companySelect?.text = data.value
    }

    fun setBoundsName(name: String) {
        this.boundsName = name
        nameEdit?.setText(name)
        nameEdit?.setSelection(name.length)
    }
}